package controllers;

import beans.EmployeeInfo;
import beans.SubcontractInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import services.impl.EmployeeServiceImpl;
import services.impl.SubcontractInfoServicesImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeServiceImpl esi;
    @Autowired
    private SubcontractInfoServicesImpl sii;

    @RequestMapping("/employee")
    public List employee() {
        List<EmployeeInfo> list = esi.selectList();
        return list;
    }

    @RequestMapping("/editemployee")
    public String editEmployee(@RequestBody EmployeeInfo employeeInfo) {
        esi.updateByPrimaryKeySelective(employeeInfo);
        return null;
    }

    @RequestMapping("/detailsemployee")
    public EmployeeInfo detailsEmployee(HttpServletRequest request) {
        int i = Integer.parseInt(request.getParameter("employeeId"));
        EmployeeInfo employeeInfo = esi.selectByPrimaryKey(i);
        System.out.println(employeeInfo);
        return employeeInfo;
    }

    @RequestMapping("/searchemployee")
    public List searchEmployee(@RequestBody Map<String,String> map) {
        String select = map.get("selectText");
        String input = map.get("inputText");
        List<EmployeeInfo> list = esi.selectSelective(select,input);
        return list;
    }

    @RequestMapping("/createemployee")
    public String createEmployee(@RequestBody EmployeeInfo employeeInfo) {
        esi.insertSelective(employeeInfo);
        return null;
    }

    @RequestMapping("/deleteemployee")
    public String deleteEmployee(HttpServletRequest request) {
        int i = Integer.parseInt(request.getParameter("employeeId"));
        esi.deleteByPrimaryKey(i);
        System.out.println("删除成功");
        return null;
    }

    @RequestMapping("/subcontract")
    public List subcontract() {
        List<SubcontractInfo> list = sii.selectList();
        return list;
    }

    @RequestMapping("/searchsubcontract")
    public List searchSubcontractFirst(HttpServletRequest request) {
        String select = request.getParameter("selectText");
        String input = request.getParameter("inputText");
        List<SubcontractInfo> list = sii.selectSelective(select,input);
        return list;
    }

    @RequestMapping("/distribution")
    public String distribution() {
        return "index";
    }

    @RequestMapping("/searchdistribution")
    public String searchDistribution() {
        return "index";
    }


}
